package edu.msg.hrappy.backend.service.util;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @author Torok Eniko This class is used for sending any kind of email to a
 *         user
 */
public class MailNotification {
	String host, port, username, password;
	static String subject;
	Properties props = System.getProperties();
	Session l_session = null;

	public MailNotification() {
		host = "smtp.mail.yahoo.com";
		port = "587";
		username = "hrappyhrappy@yahoo.ro";
		password = "Laskaszuro8";
		emailSettings();
		createSession();
	}

	public void emailSettings() {
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.auth", "true");
		props.put("mail.debug", "false");
		props.put("mail.smtp.port", port);
	}

	public void createSession() {
		l_session = Session.getInstance(props, new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		});
		l_session.setDebug(true);
	}

	public boolean sendMessage(String toEmail, String msg, String subject) {
		try {
			MimeMessage message = new MimeMessage(l_session);
			message.setFrom(new InternetAddress(username));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(
					toEmail));
			message.setSubject(subject);
			message.setContent(msg, "text/html");
			Transport.send(message);
			System.out.println("Message Sent");
		} catch (MessagingException mex) {
			mex.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}
}